package com.lczyfz.istep.common.service;
import com.lczyfz.istep.common.utils.FtpUtil;
import com.lczyfz.istep.common.utils.PropertiesUtil;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author dingh
 * @ClassName FileUploadServiceImpl
 * @Description
 * @Date 2017.2.24
 * @History :
 * <author>
 * <time>
 * <desc>
 */
@Service
public class FileUploadServiceImpl implements FileUploadService {
    static Logger logger = Logger.getLogger(FileUploadServiceImpl.class);
    String FTP_IP, FTP_USER, FTP_PASSWORD, FTP_PORT,FTP_BASE;

    @Override
    public Map<String, Object> uploadFile(String path, String filename, MultipartFile file) {
        FTP_IP=PropertiesUtil.getProperty("FTP_IP");
        FTP_USER=PropertiesUtil.getProperty("FTP_USER");
        FTP_PASSWORD=PropertiesUtil.getProperty("FTP_PASSWORD");
        FTP_PORT=PropertiesUtil.getProperty("FTP_PORT");
        FTP_BASE=PropertiesUtil.getProperty("FTP_BASE");
        FTPClient ftpClient = FtpUtil.getFtpClient(FTP_IP, Integer.parseInt(FTP_PORT), FTP_USER, FTP_PASSWORD);

        if(ftpClient.isConnected())
        {
            logger.debug("连接成功!!!!!");
            filename=getUniqueName(filename);
            boolean isSuccess= false;
            try {
                isSuccess = FtpUtil.uploadFile(ftpClient, path, filename, file.getInputStream());
                if(isSuccess)
                {
//                String ftpFileAddress = "http://" + FTP_IP + FTP_BASE + path+ "/" + filename;
                    String ftpFileAddress =FTP_BASE + path+ "/" + filename;
                    Map<String, Object> result=new HashMap<>();
                    result.put("isSuccess",isSuccess);
                    result.put("ftpFileAddress",ftpFileAddress);

                    return result;
                }else {
                    FtpUtil.closeFtp(ftpClient);
                    return null;
                }
            } catch (IOException e) {
                e.printStackTrace();
                FtpUtil.closeFtp(ftpClient);
                return null;
            }
        }
        else
        {
            logger.debug("连接失败!!!!!");
            return null;
        }






    }

    @Override
    public boolean deleteFile(String filePath) {
        FTP_IP=PropertiesUtil.getProperty("FTP_IP");
        FTP_USER=PropertiesUtil.getProperty("FTP_USER");
        FTP_PASSWORD=PropertiesUtil.getProperty("FTP_PASSWORD");
        FTP_PORT=PropertiesUtil.getProperty("FTP_PORT");
        FTP_BASE=PropertiesUtil.getProperty("FTP_BASE");
        FTPClient ftpClient = FtpUtil.getFtpClient(FTP_IP, Integer.parseInt(FTP_PORT), FTP_USER, FTP_PASSWORD);
        if(ftpClient.isConnected())
        {
            logger.debug("连接成功!!!!!");
            try {
                return  FtpUtil.deleteFile(ftpClient,filePath);
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }else{
            return false;
        }




    }

    private String getUniqueName(String fileName) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        if(fileName.lastIndexOf(".")!=-1)
            return df.format(Calendar.getInstance().getTime()) + UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf(".")); //为日期 + uuID + 后缀名
        else
            return df.format(Calendar.getInstance().getTime()) + UUID.randomUUID().toString() + fileName; //为日期 + uuID + 后缀名

    }
}
